package com.golxen.framework.event.condition;



import com.golxen.framework.event.BaseMultiEventSource;
import com.golxen.framework.event.IEventSource;
import com.golxen.framework.event.condition.dao.UserConditionDaoImpl;
import com.golxen.framework.event.condition.dao.entity.ConditionInfo;
import com.golxen.framework.event.condition.dao.entity.UserConditionData;
import com.golxen.framework.module.AbstractModule;
import com.golxen.framework.user.player.GamePlayer;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


/**
 * 任务条件模块（事件监听器使用）
 */
public class ConditionModule extends AbstractModule {

	//用户当前任务条件
	private Map<Integer, UserConditionData> userConditionDataMap;

	//用户当前任务条件和对应的任务完成条件
	private Map<Integer,UserBaseCondition> userBaseConditionMap;

	private final IEventSource gamePlayerEvent;


	public ConditionModule(GamePlayer player) {
		super(player);
		this.gamePlayerEvent = new BaseMultiEventSource();
	}

	@Override
	public boolean loadDB() {
		userBaseConditionMap=new ConcurrentHashMap<>();
		userConditionDataMap = new UserConditionDaoImpl().getUserCondition(player.getUserId());

		// 加载用户的任务
		for (Map.Entry<Integer, ConditionInfo> entry : ConditionMgr.getCondition().entrySet()) {
			addConditionToUser(entry.getValue());
		}
		return true;
	}

	@Override
	public boolean afterLoadDB() {

		return true;
	}

	@Override
	public boolean saveDB() {
		for (Map.Entry<Integer, UserConditionData> entry : userConditionDataMap.entrySet()) {
			if (null != entry.getValue()) {
				if (entry.getValue().isInsertOption()){
					new UserConditionDaoImpl().add(entry.getValue());
				}
				if (entry.getValue().isUpdateOption()) {
					new UserConditionDaoImpl().update(entry.getValue());
				}
			}
		}

		return true;
	}

	@Override
	public void loginSendMsg() {

	}

	private void addConditionToUser(ConditionInfo conditionInfo) {

		if (null == conditionInfo) {
			return;
		}
		UserConditionData userConditionData=userConditionDataMap.get(conditionInfo.getConditionId());
		if(userConditionData!=null){
			//将预先注册准备的完成条件创建起来
			BaseCondition baseCondition = ConditionPlugin.createCommonCondition(player, conditionInfo, userConditionData);
			UserBaseCondition baseTaskData = new UserBaseCondition(baseCondition, userConditionData, player);
			baseTaskData.addTrigger();
			userBaseConditionMap.put(userConditionData.getConditionId(), baseTaskData);
		}else {
			userConditionData=new UserConditionData();
			userConditionData.setUserId(player.getUserId());
			userConditionData.setConditionId(conditionInfo.getConditionId());
			userConditionData.setValue(BigInteger.valueOf(0));
			userConditionData.setParam("");
			userConditionData.setType(conditionInfo.getType());
			userConditionData.setInsertOption();
			BaseCondition baseCondition = ConditionPlugin.createCommonCondition(player, conditionInfo, userConditionData);
			UserBaseCondition baseTaskData = new UserBaseCondition(baseCondition, userConditionData, player);
			baseTaskData.addTrigger();
			userBaseConditionMap.put(userConditionData.getConditionId(), baseTaskData);
		}

	}

	/**
	 * 重置日常任务
	 */
	public void resetDay() {

	}


	public IEventSource getGamePlayerEvent() {
		return gamePlayerEvent;
	}



}
